﻿using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Gencode.Controls.Harmonica
{
	[ToolboxItem(false)]
	public class HarmonicaNodeButton : Control
	{
		public HarmonicaNodeButton()
		{
		}

		public HarmonicaNodeButton(Control parent)
		{
			Size = new Size(parent.ClientRectangle.Width, 20);
			Location = new Point(parent.Location.X, parent.Location.Y);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);

			Graphics g = e.Graphics;

			StringFormat sf = new StringFormat();
			sf.Alignment = StringAlignment.Center;//.Center;
			sf.LineAlignment = StringAlignment.Center;//.Center;

			g.FillRectangle(BrushColor, Rect);
			//g.DrawString(Text, Font, Brushes.Black, 10,10);
			g.DrawString(Text, Font, Brushes.Black, new RectangleF(Location.X, Location.Y, Size.Width, Size.Height), sf); //(RectangleF)Rect, sf);
			//g.DrawString(Text, sender.Font, Brushes.Black, new RectangleF(Location.X, Location.Y, Size.Width, Size.Height));//, sf);

			Rectangle r = Rect;
			//r.Inflate(-4, -4);
			r.Width--;
			r.Height--;
			Pen p = new Pen(Color.Black, 1);
			p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
			g.DrawRectangle(p, r);
		}
		//internal void HarmonicaButton_Paint(Control sender, Graphics g)
		//{
		//    StringFormat sf = new StringFormat();
		//    sf.Alignment = StringAlignment.Center;//.Center;
		//    sf.LineAlignment = StringAlignment.Center;//.Center;

		//    g.FillRectangle(BrushColor, Rect);
		//    //g.DrawString(Text, Font, Brushes.Black, 10,10);
		//    g.DrawString(Text, sender.Font, Brushes.Black, new RectangleF(Location.X, Location.Y, Size.Width, Size.Height), sf); //(RectangleF)Rect, sf);
		//    //g.DrawString(Text, sender.Font, Brushes.Black, new RectangleF(Location.X, Location.Y, Size.Width, Size.Height));//, sf);

		//    Rectangle r = Rect;
		//    //r.Inflate(-4, -4);
		//    r.Width--;
		//    r.Height--;
		//    Pen p = new Pen(Color.Black, 1);
		//    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
		//    g.DrawRectangle(p, r);
		//}

		Rectangle rect;
		private Rectangle Rect
		{
			get
			{
				return rect;
			}
			set
			{
				rect = value;
			}
		}

		//Size size;
		//public override Size Size
		//{
		//    get
		//    {
		//        if (size == null)
		//            size = new Size(80, 20);
		//        return size;
		//    }
		//    set
		//    {
		//        size = value;
		//    }
		//}

		//Point location;
		//public override Point Location
		//{
		//    get
		//    {
		//        if (location == null)
		//        {
		//            location = new Point(0, 0);
		//            RecreateRectangle();
		//        }
		//        return location;
		//    }
		//    set
		//    {
		//        location = value;
		//        RecreateRectangle();
		//    }
		//}

		Brush brushColor;
		Brush BrushColor
		{
			get
			{
				if (brushColor == null)
				{
					brushColor = new SolidBrush(BackColor);
					RecreateRectangle();
				}
				return brushColor;
			}

		}

		//Color backColor = Color.FromName("Control");

		////[DefaultValue(Color.FromName("Control"))]
		//public Color BackColor
		//{
		//    get
		//    {
		//        if (backColor == null)
		//        {
		//            backColor = Color.FromName("Control");
		//        }
		//        return backColor;
		//    }
		//    set
		//    {
		//        brushColor = null;
		//        backColor = value;
		//    }
		//}

		//string text = "";
		//public string Text
		//{
		//    get
		//    {
		//        return text;
		//    }
		//    set
		//    {
		//        text = value;
		//    }
		//}

		private void RecreateRectangle()
		{
			Rect = new Rectangle(Location, Size);
		}
	}
}
